﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlobalResources;
using System.Web;
using Googler.BLL;
using HodhodNews.GlobalResources;

namespace HodhodNews
{
    public class NewsRenderer : INewsRenderer
    {
        const string html = "<HTML dir=rtl><body>{0}</body></HTML>";
        const string itemLinkStyle = "style='font-family:tahoma;font-size:9pt'";
        const string itemHtmlTemplate = "<hr /><b>{0}</b>" +
              "&nbsp;&nbsp;{1}&nbsp;&nbsp;{2}" +
              "<h2>{3}</h2><div>{4}</div>";

        string RenderItemDeleteLink(long newsId)
        {
            return "<a onclick='return confirm(\"" + Messages.ConfirmDeleting + "\");' " + itemLinkStyle +
                   string.Format("href='http://newsadmin/execute.aspx?command=delete&newsid={0}'>",newsId) +
                   Common.Delete + "</a>";
        }
        string RenderItemPreviewLink(long newsId)
        {
            return "<a " + itemLinkStyle +
                   string.Format("href='http://newsadmin/execute.aspx?command=preview&newsid={0}'>", newsId) +
                   Common.Preview + "</a>";
        }
        public string Render(List<News> list)
        {
            string content = "";
            foreach (News atom in list)
            {
                content += this.Render(atom);
            }
            return string.Format(html, content);
        }
        public string Render(News item)
        {
            return string.Format(itemHtmlTemplate,
                                 item.OId,
                                 RenderItemDeleteLink(item.OId),
                                 RenderItemPreviewLink(item.OId),
                                 item.Title,
                                 item.SummaryHtml);
        }
        public string Render(News item, bool fullArticle)
        {
            if (!fullArticle)
                return Render(item);

            string itemString = string.Format(itemHtmlTemplate,
                item.OId,
                RenderItemDeleteLink(item.OId),
                RenderItemPreviewLink(item.OId),
                item.Title,
                item.Text );

            return string.Format(html, itemString);
        }



        public NewsCommand ParseCommand(Uri uri)
        {
            var loweredQueryString = uri.Query.ToLower();
            var queryString = HttpUtility.ParseQueryString(loweredQueryString);
            var newsCommand = new NewsCommand();
            int id = 0;
            string strID = queryString["newsid"];
            if (int.TryParse(strID, out id))
            {
                newsCommand.OId = id;
            }

            if (queryString["command"] == "delete")
            {
                newsCommand.Type = NewsCommandType.Delete;                
            }
            else if (queryString["command"] == "preview")
            {
                newsCommand.Type = NewsCommandType.Preview;
            }
            else if (uri.AbsoluteUri.StartsWith("http://news.google.com"))
            {
                newsCommand.Type = NewsCommandType.GoingToGoogle;
            }
            else
            {
                newsCommand.Type = NewsCommandType.Unknown;
            }
            return newsCommand;
        }
    }
}
